/*
 * @lc app=leetcode.cn id=14 lang=javascript
 *
 * [14] 最长公共前缀
 */

// @lc code=start
/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function (strs) {
  if (strs.length <= 1) {
    return strs[0] || ''
  }
  let prefix = ''
  const first = strs[0]
  const len = first.length
  for (let i = 0; i < len; i++) {
    const chart = first[i]
    for (let index = 1; index < strs.length; index++) {
      let strItem = strs[index]
      if (!strItem[i] || strItem[i] != chart) {
        return prefix
      }
    }
    prefix += chart
  }
  return prefix
}
// @lc code=end

console.log(longestCommonPrefix(['flower', 'flow', 'flight']))
console.log(longestCommonPrefix(['dog', 'racecar', 'car']))
